package com.cug.campus_sh_market_server.controller;


import com.cug.campus_sh_market_server.entity.param.OrderParam;
import com.cug.campus_sh_market_server.entity.param.PageParam;
import com.cug.campus_sh_market_server.entity.param.goods.GoodsAddParam;
import com.cug.campus_sh_market_server.entity.param.goods.GoodsPutParam;
import com.cug.campus_sh_market_server.entity.param.goods.GoodsQueryParam;
import com.cug.campus_sh_market_server.entity.param.goods.GoodsStatusUpdateParam;
import com.cug.campus_sh_market_server.entity.param.user.UserStatusUpdateParam;
import com.cug.campus_sh_market_server.entity.po.Goods;
import com.cug.campus_sh_market_server.entity.po.User;
import com.cug.campus_sh_market_server.entity.vo.ResultData;
import com.cug.campus_sh_market_server.entity.vo.goods.GoodsAdminVO;
import com.cug.campus_sh_market_server.entity.vo.goods.GoodsVO;
import com.cug.campus_sh_market_server.service.GoodsService;
import com.nycreativity.elecreativity.entity.vo.PageDataVO;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.cug.campus_sh_market_server.common.constant.UrlConstants.GOODS_PREFIX;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author sxt
 * @since 2022-03-22
 */
@RestController
@RequestMapping(GOODS_PREFIX)
public class GoodsController
        extends AbsPreAuthorizeRestfullController<Goods, GoodsQueryParam, GoodsAddParam, GoodsPutParam, GoodsVO> {

    @Resource
    private GoodsService goodsService;

    @PreAuthorize("permitAll()")
    @Override
    protected ResultData<PageDataVO<GoodsVO>> getDataByPage(GoodsQueryParam goodsQueryParam, PageParam pageParam, OrderParam orderParam) {
        return super.getDataByPage(goodsQueryParam, pageParam, orderParam);
    }

    @PreAuthorize("permitAll()")
    @Override
    protected ResultData<List<GoodsVO>> getData(GoodsQueryParam goodsQueryParam, OrderParam orderParam) {
        return super.getData(goodsQueryParam, orderParam);
    }

    @GetMapping("admin/list")
    @ApiOperation("分页查询所有商品数据(管理端用)")
    public ResultData<PageDataVO<GoodsAdminVO>> getAdminGoodsByPage(GoodsQueryParam goodsQueryParam, PageParam pageParam, OrderParam orderParam) {
        return ResultData.SUCCESS("成功", goodsService.queryAdminGoodsByPage(goodsQueryParam, pageParam, orderParam));
    }

    @PutMapping("changeGoodsStatus")
    @ApiOperation(value = "修改商品上架状态")
    public ResultData<Object> changeGoodsStatus(@RequestBody GoodsStatusUpdateParam goodsStatusUpdateParam) {
        boolean rs = goodsService.changeGoodsStatus(goodsStatusUpdateParam);
        return rs ? ResultData.SUCCESS("操作成功") : ResultData.FAILED("操作失败");
    }

}
